package com.weixing.mall.provider.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.provider.mapper.DayReportAdvertisementMapper;
import com.weixing.mall.provider.model.domain.Order;
import com.weixing.mall.provider.model.enums.AppointmentEunm;
import com.weixing.mall.provider.model.vo.report.advertisement.*;
import com.weixing.mall.provider.service.IDayReportAdvertisementService;
import org.springframework.stereotype.Service;

/**
 * @author jzy
 * @date 2020/6/6
 */
@Service
public class DayReportAdvertisementServiceImpl extends ServiceImpl<DayReportAdvertisementMapper, Order> implements IDayReportAdvertisementService {

    @Override
    public IPage<AdvertisementDetailVo> advertisementPage(String chatNum, String startTime, String endTime, Page page) {
        QueryWrapper<Order> q = new QueryWrapper<>();
        q.select("ads.id,mediaType.media_name,platform.plateform_name,platform.chat_num,ads.page_site pageSite,ads.launch_date,ads.end_order_date" +
                ",agent.agent_name orderedAgent,ads.staus,ads.price,ads.article_title,ads.read_amount,ads.fans");
        q.like(StrUtil.isNotEmpty(chatNum),"platform.chat_num",chatNum);
        q.ge(StrUtil.isNotEmpty(startTime),"date_format(ads.launch_date,'%Y-%m-%d')",startTime);
        q.le(StrUtil.isNotEmpty(endTime),"date_format(ads.launch_date,'%Y-%m-%d')",endTime);
        // TODO REPORT 投放状态待确定 筛选已投放的
        q.eq("record.status", AppointmentEunm.STATUS8.getValue());
        return baseMapper.advertisementPage(page, q);
    }

    @Override
    public IPage<AdvertisementServiceDataVo> advertisementServiceData(String agentName,Long directorId,String wechatNum, String chatNum, String startTime, String endTime, Page page) {
        QueryWrapper<Order> q = new QueryWrapper<>();
        q.select("platform.plateform_name,platform.chat_num,ads.launch_date,ads.price,platform.price_in,platform.price_out" +
                ",agent.agent_name,agent.wechat_num,ads.staus,ads.article_title,ads.read_amount,ads.fans,su.real_name directorName");
        q.like(StrUtil.isNotEmpty(wechatNum),"agent.wechat_num",wechatNum);
        q.like(StrUtil.isNotEmpty(agentName),"agent.agent_name",agentName);
        q.like(StrUtil.isNotEmpty(chatNum),"platform.chat_num",chatNum);
        q.eq("ads.director",directorId);
        q.ge(StrUtil.isNotEmpty(startTime),"date_format(ads.launch_date,'%Y-%m-%d')",startTime);
        q.le(StrUtil.isNotEmpty(endTime),"date_format(ads.launch_date,'%Y-%m-%d')",endTime);
        // TODO REPORT 投放状态待确定 筛选已投放的
        q.eq("record.status", AppointmentEunm.STATUS8.getValue());
        return baseMapper.advertisementServiceDataPage(page, q);
    }

    @Override
    public IPage<AdvertisementLaunchReportVo> advertisementLaunchReport(String directorName, String startTime, String endTime, Page page) {
        QueryWrapper<Order> q = new QueryWrapper<>();
        q.select("sysUser.real_name directorName" +
                ",count(if(record.status="+AppointmentEunm.STATUS8.getValue()+",true,null)) launchQuantity" +
                ",IFNULL(sum(if(record.status="+AppointmentEunm.STATUS8.getValue()+",ads.price,0)),0) launchAmount" +
                ",count(if(record.status="+AppointmentEunm.STATUS7.getValue()+",true,null)) layoutQuantity" +
                ",IFNULL(sum(if(record.status="+AppointmentEunm.STATUS7.getValue()+",ads.price,0)),0) layoutAmount");
        q.like(StrUtil.isNotEmpty(directorName),"sysUser.real_name",directorName);
        q.ge(StrUtil.isNotEmpty(startTime),"date_format(ads.launch_date,'%Y-%m-%d')",startTime);
        q.le(StrUtil.isNotEmpty(endTime),"date_format(ads.launch_date,'%Y-%m-%d')",endTime);
        // TODO REPORT 投放状态待确定 筛选已投放的
        //筛选已投放和已排版的广告
        q.and(i->i.eq("record.status", AppointmentEunm.STATUS8.getValue()).or().eq("record.status", AppointmentEunm.STATUS7.getValue()));
        q.groupBy("ads.director");
        return baseMapper.advertisementLaunchReportPage(page,q);
    }

    @Override
    public IPage<AdvertisementDirectorPerformanceVo> advertisementDirectorPerformance(String directorName, String startTime, String endTime, Page page) {
        QueryWrapper<Order> q = new QueryWrapper<>();
        q.select("date_format(ads.launch_date,'%Y-%m') month,sysUser.real_name directorName" +
                ",count(0) quantity" +
                ",sum(ads.fans) fans" +
                ",round(avg(ads.fans),2) avgFans" +
                ",sum(ads.price) amount" +
                ",round(avg(ads.price),2) avgAmount" +
                ",round((sum(ads.price)/sum(ads.fans)),2) avgCost" +
                "");
        q.like(StrUtil.isNotEmpty(directorName),"sysUser.real_name",directorName);
        q.ge(StrUtil.isNotEmpty(startTime),"date_format(ads.launch_date,'%Y-%m-%d')",startTime);
        q.le(StrUtil.isNotEmpty(endTime),"date_format(ads.launch_date,'%Y-%m-%d')",endTime);
        // TODO REPORT 投放状态待确定 筛选已投放的
        q.eq("record.status", AppointmentEunm.STATUS8.getValue());
        q.groupBy("date_format(ads.launch_date,'%Y-%m')","ads.director");
        return baseMapper.advertisementDirectorPerformance(page,q);
    }

    @Override
    public IPage<AdvertisementPlatformPerformanceVo> advertisementPlatformPerformance(String yearMonth,String platformName,Long platform, Integer pageSite, String startTime, String endTime, Page page) {
        QueryWrapper<Order> q = new QueryWrapper<>();
        q.select("date_format(ads.launch_date,'%Y-%m-%d') day,date_format(ads.launch_date,'%Y-%m') month" +
                ",platform.plateform_name,ads.page_site" +
                ",count(0) quantity" +
                ",sum(ads.fans) fans" +
                ",round(avg(ads.fans),2) avgFans" +
                ",sum(ads.price) amount" +
                ",round(avg(ads.price),2) avgAmount" +
                ",round((sum(ads.price)/sum(ads.fans)),2) avgCost" +
                "");
        q.eq(platform!=null,"ads.platform",platform);
        q.eq(pageSite!=null,"ads.page_site",pageSite);
        q.ge(StrUtil.isNotEmpty(startTime),"date_format(ads.launch_date,'%Y-%m-%d')",startTime);
        q.le(StrUtil.isNotEmpty(endTime),"date_format(ads.launch_date,'%Y-%m-%d')",endTime);
        // TODO REPORT 投放状态待确定 筛选已投放的
        q.eq("record.status", AppointmentEunm.STATUS8.getValue());
        q.likeLeft(StrUtil.isNotEmpty(platformName),"platform.plateform_name",platformName);
        q.having(StrUtil.isNotEmpty(yearMonth),"month='"+yearMonth+"'");
        q.groupBy("day","month","ads.platform","ads.page_site");
        return baseMapper.advertisementPlatformPerformance(page,q);
    }

    @Override
    public IPage<AdvertisementPlatformPerformanceVo> advertisementPlatformPerformanceMonth(Long platform, Integer pageSite, String yearMonth, Page page) {
        QueryWrapper<Order> q = new QueryWrapper<>();
        q.select("date_format(ads.launch_date,'%Y-%m') month,platform.plateform_name,ads.page_site" +
                ",count(0) quantity" +
                ",sum(ads.fans) fans" +
                ",round(avg(ads.fans),2) avgFans" +
                ",sum(ads.price) amount" +
                ",round(avg(ads.price),2) avgAmount" +
                ",round((sum(ads.price)/sum(ads.fans)),2) avgCost" +
                "");
        q.eq(platform!=null,"ads.platform",platform);
        q.eq(pageSite!=null,"ads.page_site",pageSite);
        q.eq(StrUtil.isNotEmpty(yearMonth),"date_format(ads.launch_date,'%Y-%m')",yearMonth);
        // TODO REPORT 投放状态待确定 筛选已投放的
        q.eq("record.status", AppointmentEunm.STATUS8.getValue());
        q.groupBy("date_format(ads.launch_date,'%Y-%m')","ads.platform","ads.page_site");
        return baseMapper.advertisementPlatformPerformance(page,q);
    }
}
